/*
 * Copyright (c) 2010, Katholieke Universiteit Leuven
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of the Katholieke Universiteit Leuven nor the names of
 *       its contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "contiki.h"
#include "looci.h"

#include "reconfiguration/rci_remote.h"

#ifdef LOOCI_HELLO_SENDER_DEBUG
#include <stdio.h>
#ifdef CONTIKI_TARGET_AVR_RAVEN
#include <avr/pgmspace.h>
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
#else
#define PRINTF printf
#endif // CONTIKI_TARGET_AVR_RAVEN
#else
#define PRINTF(...)
#endif // LOOCI_HELLO_SENDER_DEBUG

// printf alike things are currently broken for loadable components.
// Your mileage may vary, so you can try to uncomment this, but don't
// rely on it working.
#ifdef BUILD_COMPONENT
#undef PRINTF
#define PRINTF(...)
#endif

#define HELLO_EVENT 100

COMPONENT_INTERFACES(sender, HELLO_EVENT);
COMPONENT_NO_RECEPTACLES(sender);
COMPONENT(sender, "Sender component");

LOOCI_COMPONENTS(&sender);

static struct etimer et;

struct activate_request {
  u8_t seq_nr;
  u8_t command;
  u8_t cid;
};

struct activate_reply {
  u8_t seq_nr;
  bool success;
};

COMPONENT_THREAD(sender, ev, data)
{
  COMPONENT_BEGIN();

  PRINTF("Sender started\r\n");

  PROCESS_PAUSE();

  // Demo of the runtime control and introspection
  // Activate the receiver
  u8_t receiver_node[] = \
    {0xfd, 0xdb, 0xe5, 0xed, 0x6f, 0xd8, 0x00, 0x01, 0x00, 0x11, 0x22, 0xff, 0xfe, 0x33, 0x44, 0x56};
  static u8_t buf[sizeof(struct activate_request)];
  struct activate_request * request = (struct activate_request *)buf;
  request->command = COMMAND_ACTIVATE;
  request->cid = 1;
  peer_id_t peer = peer_get_id_or_add((peer_addr_t *)receiver_node);
  looci_rci_remote_execute(peer, buf, 3);
  // process reply
  static u8_t len = 3;
  memset(buf, 0, sizeof(buf));
  LOOCI_RCI_REMOTE_WAIT_REPLY(buf, &len);
  // End demo
  
  while(((struct activate_reply *) buf)->success) {
    etimer_set(&et, 5 * CLOCK_SECOND);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

    looci_event_publish(HELLO_EVENT, "Hello world!", 13);

    PRINTF("Sender: event published\r\n");
  }

  COMPONENT_END();
}

